package com.meihua.shellapp.fragment.order;

import android.support.v7.widget.LinearLayoutManager;

import com.aspsine.irecyclerview.IRecyclerView;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.adapter.personalcenter.OrderFragmentAdapter;
import com.meihua.shellapp.base.BaseFragment;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.MyOrderBean;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.footer.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/26.
 * 邮箱：lc0607live@163.com
 */

public class OrderFragment extends BaseFragment {

    public int type;
    private OrderFragmentAdapter mAdapter;
    private LoadMoreFooterView loadMoreFooterView;
    private List<MyOrderBean.DataBean> mData = new ArrayList<>();
    private int page = 1;
    private String startTimeStr;//开始时间
    private String endTimeStr;//结束时间
    private String policy_holder;//投保人姓名
    private String recognizee;//被保人姓名
    private String order_num;//订单号
    private String typeIndex;//类型
    private boolean isvisible;

    public OrderFragment setType(int type) {
        this.type = type;
        return this;
    }

    @Bind(R.id.rv)
    IRecyclerView rv;//列表

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_layout_rv;
    }

    @Override
    protected void initView() {
        intList();
    }

    /**
     * 初始化列表
     */
    private void intList() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        rv.setLayoutManager(layoutManager);
        loadMoreFooterView = (LoadMoreFooterView) rv.getLoadMoreFooterView();
        mAdapter = new OrderFragmentAdapter(getActivity(), mData);
        rv.setIAdapter(mAdapter);
        rv.setOnLoadMoreListener(this);
        rv.setOnRefreshListener(this);
    }

    /**
     * 设置筛选条件
     *
     * @param startTimeStr  开始时间
     * @param endTimeStr    结束时间
     * @param policy_holder 投保人姓名
     * @param recognizee    被保人姓名
     * @param order_num     订单号
     * @param typeIndex     类型  1.寿险  2 车险  3 其他
     */
    public void setScreenCondition(String startTimeStr, String endTimeStr, String policy_holder, String recognizee, String order_num, int typeIndex) {
        this.startTimeStr = startTimeStr.equals("开始日期") ? null : startTimeStr;
        this.endTimeStr = endTimeStr.equals("结束日期") ? null : endTimeStr;
        this.policy_holder = policy_holder.equals("") ? null : policy_holder;
        this.recognizee = recognizee.equals("") ? null : recognizee;
        this.order_num = order_num.equals("") ? null : order_num;
        this.typeIndex = typeIndex == 0 ? null : typeIndex + "";
        page = 1;
        if (isvisible) {
            getData();
        }
    }

    @Override
    public void onLoadMore() {
        super.onLoadMore();
        if (loadMoreFooterView.canLoadMore() && mAdapter.getItemCount() > 0) {
            loadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
            page++;
            getData();
        }

    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        page = 1;
        getData();
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            isvisible = true;
            getData();
        } else {
            isvisible = false;
        }
    }

    /**
     * 获取我的订单列表
     */
    private void getData() {
        OkGo.<String>post(URL.getUrl().GETORDER)
                .params("member_id", MyApplication.getInstance().getUserId())
                .params("pay_status", type)
                .params("ins_status", typeIndex)
                .params("policy_holder_name", policy_holder)
                .params("insured_person_name", recognizee)
                .params("order_num", order_num)
                .params("insurance_star", startTimeStr)
                .params("insurance_end", endTimeStr)
                .params("page", page)
                .execute(new MyStringCallBack<MyOrderBean>(rv) {

                    @Override
                    public void onSuccessData(MyOrderBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null && bean.getData().size() > 0) {
                            if (page == 1) {
                                mData.clear();
                            }
                            mData.addAll(bean.getData());
                            mAdapter.notifyDataSetChanged();
                        } else {
                            if (page == 1) {
                                mData.clear();
                                mAdapter.notifyDataSetChanged();
                            }
                            endRefreshing(rv);
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }
}
